<?php

class UserQuery extends Doctrine_Query
{
  protected $is_projects_joined = false;
  protected $is_tickets_to_me_joined = false;
  protected $is_tickets_from_me_joined = false;
  protected $is_groups_joined = false;
  protected $is_permissions_joined = false;
  protected $is_teams_joined = false;

  /**
   * Instantiates UserQuery class
   *
   * @param Doctrine_Connection $conn
   * @param string $class
   * @return UserQuery
   */
  static public function create($conn = null, $class = null)
  {
    return parent::create($conn, 'UserQuery')->from('sfGuardUser u')->leftJoin('u.Profile p');
  }
  

  public function joinProjects()
  {
    if (!$this->is_projects_joined)
    {
      $this->leftJoin('p.Projects prj');
      $this->is_projects_joined = true;
    }

    return $this;
  }


  public function joinGroups()
  {
    if (!$this->is_groups_joined)
    {
      $this->leftJoin('u.groups ug');
      $this->is_groups_joined = true;
    }

    return $this;
  }
  
  public function joinPermissions()
  {
    if (!$this->is_permissions_joined)
    {
      $this->joinGroups()->leftJoin('u.permissions uup')->leftJoin('ug.permissions ugp');
      $this->is_permissions_joined = true;
    }

    return $this;
  }
  
  public function filterByPermissions($permissions)
  {
    if (!is_array($permissions))
      $permissions=array($permissions);
      
    if (!count($permissions))
      return $this;
      
    foreach ($permissions as $i=>$p)
      $permissions[$i]='\''.$p.'\'';
    
    $permissions=implode(',',$permissions);
      
    return $this->joinPermissions()->andWhere('uup.name IN ('.$permissions.') OR ugp.name IN ('.$permissions.')');
  }
  
  public function joinTeams()
  {
    if (!$this->is_teams_joined)
    {
      $this->leftJoin('u.Teams t');
      $this->is_teams_joined = true;
    }

    return $this;
  }
  
  public function joinTicketsToMe()
  {
    if (!$this->is_tickets_to_me_joined)
    {
      $this->leftJoin('u.TicketsToMe ttm');
      $this->is_tickets_to_me_joined = true;
    }

    return $this;
  }
  
  public function joinTicketsFromMe()
  {
    if (!$this->is_tickets_from_me_joined)
    {
      $this->leftJoin('u.TicketsFromMe tfm');
      $this->is_tickets_from_me_joined = true;
    }

    return $this;
  }
  
  public function filterByProjectId($project_id)
  {
    return $this->joinProjects()->andWhere('prj.id = ?',$project_id);
  }
  
  public function filterByTeamId($team_id)
  {
    return $this->andWhere('t.id = ?',$team_id);
  }
  
  
  /**   Выбирает только тех пользователей, с которыми $user_id может общаться 
    *
    */  
  public function onlyAvailableInteractWith($user_id=null)
  {
    if (!$user_id)
      return $this;
      
    //Если сушествующий, то теоретически может общаться только с теми, кто привязан к тем же проектам, что и он сам
    return $this->joinProjects()->leftJoin('prj.Users prju')->andWhere('u.id != ?',$user_id)->andWhere('prju.id != ?',$user_id);
  }
  
  /** Выбирает только тех пользователей, которым виден тикет $ticket_id
    *
    *
    */
  function filterByVisibleInTicket($ticket_id)
  {
    //TODO
  }
  
}
